using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Linq;
using System.IO;
using ESRI.ArcGIS.SystemUI;
using ESRI.ArcGIS.Framework;
using ESRI.ArcGIS.ADF.CATIDs;
using esriSystem = ESRI.ArcGIS.esriSystem;
using Infragistics.Win.UltraWinDock;
using BesAsm.Emgaats.Core;
using BesAsm.Emgaats.Core.Commands;
using BesAsm.Emgaats.Tools;
using BesAsm.Framework.CommonControls;
using BesAsm.Framework.Reporting;
using BesAsm.ModelData.Entities;

namespace BesAsm.Emgaats.Core
{
  [ClassInterface(ClassInterfaceType.None)]
  public partial class UIEmgaatsDockableWindow : UserControl
  {
    private EmgaatsCoreExtension _emgExtension;

    /// <summary>
    /// Returns the ArcGIS ProgID
    /// </summary>  

    public UIEmgaatsDockableWindow(object hook)
    {
      InitializeComponent();

      this.Hook = hook;

      try
      {
        _emgExtension = EmgaatsCoreExtension.GetExtension();
      }
      catch (Exception ex)
      {

      }
    }

    /// <summary>
    /// Host object of the dockable window
    /// </summary>
    private object Hook
    {
      get;
      set;
    }

    /// <summary>
    /// Implementation class of the dockable window add-in. It is responsible for 
    /// creating and disposing the user interface class of the dockable window.
    /// </summary>
    public class AddinImpl : ESRI.ArcGIS.Desktop.AddIns.DockableWindow
    {
      private UIEmgaatsDockableWindow m_windowUI;

      public AddinImpl()
      {
      }

      protected override IntPtr OnCreateChild()
      {
        m_windowUI = new UIEmgaatsDockableWindow(this.Hook);
        return m_windowUI.Handle;
      }

      protected override void Dispose(bool disposing)
      {
        if (m_windowUI != null)
          m_windowUI.Dispose(disposing);

        base.Dispose(disposing);
      }

    }

    /// <summary> 
    /// Clean up any resources being used.
    /// </summary>
    /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
    protected override void Dispose(bool disposing)
    {
      if (disposing && (components != null))
      {
        components.Dispose();
      }
      Properties.Settings.Default.Save();
      base.Dispose(disposing);

    }

    private void btnShowMessageLog_Click(object sender, EventArgs e)
    {
      System.Diagnostics.Process process = new System.Diagnostics.Process();
      process.StartInfo.FileName = Mediator.LogFileName;

      process.Start();
    }

    private void messengerUserControl1_ErrorMessageReported(object sender, MessageReportedArgs e)
    {
      
    }

    public void UpdateModelInfo()
    {
      //TODO: Handle to model changed events and call this method
      ultraStatusBar1.Panels["CurrentModel"].Text =
        String.Format("Current model: {0}, {1}",
        Mediator.Model.GetPathIfSet(ModelPathComponent.RootFolder),
        System.IO.Path.GetFileName(Mediator.Model.GetPathIfSet(ModelPathComponent.ConfigFile)));

      Model model = Mediator.Model;
      if (model.State >= ModelState.Assembled)
      {

        lblNumDscs.Text = String.Format("{0}", model.Dscs == null ? 0 : model.Dscs.Count);
        lblNumLinks.Text = String.Format("{0}", model.Links == null ? 0 : model.Links.Count);
        lblNumNodes.Text = String.Format("{0}", model.Nodes == null ? 0 : model.Nodes.Count);
        lblNumSscs.Text = String.Format("{0}", model.Sscs == null ? 0 : model.Sscs.Count);
        lblNumParkingTargets.Text = String.Format("{0}", model.ParkingTargets == null ? 0 : model.ParkingTargets.Count);
        lblNumRoofTargets.Text = String.Format("{0}", model.RoofTargets == null ? 0 : model.RoofTargets.Count);
        lblNumStreetTargets.Text = String.Format("{0}", model.StreetTargets == null ? 0 : model.StreetTargets.Count);
      }
    }

  }
}
